<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Blog
 *
 * @author thanhdx
 */
class BlogModel extends DB {

    protected $id;
    protected $title;
    protected $teaser;
    protected $body;
    protected $entry_status;
    protected $created;
    protected $modify;
    protected $user_id;

    public function __set($name, $value) {
        if (property_exists($this, $name)) {
            $this->$name = $value;
        } else {
            throw new Exception();
        }
    }

    public function __get($name) {
        if (property_exists($this, $name)) {
            return $this->$name;
        }
        return null;
    }

    public function load($id) {
        $query = 'SELECT * FROM ' . TBL_ENTRIES . ' WHERE id = ' . $id;
        $stmt = $this->prepare($query);
        $stmt->execute();
        $data = $stmt->fetch();
        if ($data) {
            foreach ($data as $name => $value) {
                if (property_exists($this, $name))
                    $this->$name = $value;
            }
        }
        return $this;
    }

    public function getData(){
        return get_object_vars($this);
    }
    public function save() {
        if ($this->id) {
            return $this->update();
        }

        $data = get_object_vars($this);
        foreach ($data as $name => $value) {
            if ($value) {
                $fields[] = $name;
                $vals[] = $value;
            }
        }
        try {
            $query = "INSERT INTO ".TBL_ENTRIES."(".implode(",", $fields).") VALUES(".  implode(",", $vals).")";
            $stmt = $this->prepare($query);
            if($stmt->execute()){
                return true;
            }
        }catch(PDOException $e){
            print $e->getTrace();
        }
        
    }

    public function delete() {
        
    }

    public function update() {
        
    }

}

?>
